package com.freesky.framework.web.service;

import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendAlarmService {

    @Autowired
    private SendMailService sendMailService;

    private static final long INTERVAL = 10 * 1000 * 60;
    private long lastAlarmTime = 0;

    private boolean canAlarm() {
        long now = System.currentTimeMillis();
        if (now - lastAlarmTime >= INTERVAL) {
            lastAlarmTime = now;
            return true;
        } else {
            return false;
        }
    }

    public void sendAlarmMail(String destAddr,String title, String content) {
        if(StringUtils.isEmpty(destAddr)){
            return;
        }
        if(!canAlarm()){
            return ;
        }

        String destAddrArr[] = destAddr.split(",");
        for(int i=0;i<destAddrArr.length;i++){
            sendMailService.sendSimpleMail(destAddrArr[i],title,content);
        }
    }

}
